<?php

namespace catchAdmin\phone\controller;
use catchAdmin\vr\model\VideoCate;
use catchAdmin\phone\model\Code;
use catchAdmin\vr\model\Videos;
use catcher\base\CatchController;
use catcher\CatchResponse;

class AppApi extends CatchController
{

    public function getList() {
        $id = request()->get('id');
        $list = Videos::where('status', 1)->where('cateid', $id)->select();
        $data = [];
        foreach ($list as $v) {
            $tmp = [
                "srcList" => [
                    [ "title" => '高清', "src" =>  $v->file  ]
                ],
                "danmuList" => [],
                "title"=> $v->title,
                "initialTime" => 100,
                "gDuration" => 1162,
                "download" => false,
                "downloading" => false,
                'downloadObj' => [],
                'image' => $v->image,
                'id' => $v->id
            ];
            $data[] = $tmp;
        }
        return CatchResponse::successLe($data);
    }

    public function getVideoList() {
        $id = request()->get('id');
        $list = Videos::where('status', 1)->where('cateid', $id)->select();
        return CatchResponse::successLe($list);
    }

    public function cateList() {
        $list = VideoCate::where('status', 1)->select();
        return CatchResponse::successLe($list);
    }

    public function checkCode() {
        $post = request()->post();
        $deviceid = @$post['deviceid'];
        $code = Code::where('code', $post['code'])->find();
        if (!$code) return CatchResponse::fail("卡号不存在");

        $video = Videos::find($post['id']);
        if (!$video) return CatchResponse::fail("视频不存在");

        if ($code->status == 0) {
            // 未使用

            // 判断是否使用的超过两个了

            if ($code->userid != 0){
                $count = Code::where('userid', $code->userid)->where('status', 1)->whereLike('use_time', "%" . date('Y-m-d', time()))->count();
                if ($count > 1) {
                    return CatchResponse::fail("您今天已经使用过两次");
                }
            }

            $code->use_time = date('Y-m-d H:i:s', time());
            $code->status = 1;
            $code->deviceid = $deviceid;
            $code->videoid = $video->id;
            $code->save();
            return CatchResponse::successLe('验证成功');
        }
        // 判断是否是一个视频
        if ($code->videoid != $video->id) return CatchResponse::fail("视频没有权限");
        // 判断是否是一个设备
        if ($code->deviceid != $deviceid) return CatchResponse::fail("设备没有权限");
        // 已使用的判断时间
        $use_time = strtotime($code->use_time);
        // 一小时以内
        if ((time() - $use_time ) > 60*60) {
            return CatchResponse::fail("卡号已过期");
        }
        // 判断是否是一个视频
        if ($code->videoid != $video->id) return CatchResponse::fail("视频没有权限");
        // 判断是否是一个设备
        if ($code->deviceid != $deviceid) return CatchResponse::fail("设备没有权限");
        return CatchResponse::successLe('验证成功');
    }

    public function version() {
        $version = 3;
        $data = [
            'versionCode' => $version,
            'versionName' => '1.0.3',
            'url' => '/apk/vr' . $version . '.apk',
            'content' => '更新了一些内容'
        ];

        return CatchResponse::successLe($data);
    }
}